<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>系统资源管理页面</title>
<script type="text/javascript" src="${path}/js/scripts/bootstart.js"></script>
<style>
.function-item {
	margin-left: 5px;
	margin-right: 5px;
}

.function-item input {
	vertical-align: bottom;
}
</style>
</head>
<body style="width: 98%; height: 90%;">
	<form id="roleForm" method="post">
		<input name="role.roleid" class="mini-hidden" id="roleid"
			value="${param. roleid}" />
		<div style="padding: 5px;">
			<table>
				<tr>
					<td style="width: 100%;">
						<div id="treegrid1" class="mini-treegrid" onload="load()"
							style="width: 100%; height: auto;" autoCheckParent="true"
							showCheckBox="true" url="${path}/resource/mgr/loadResource"
							treeColumn="name" idField="sourceId" parentField="parentId"
							dataField="list" resultAsTree="false" allowResize="true"
							expandOnLoad="true" showTreeIcon="true" multiSelect="true"
							allowSelect="true" allowCellSelect="true" enableHotTrack="true"
							ondrawcell="ondrawcell">
							<div property="columns">
								<div name="name" field="sourceName" width="220">模块名称</div>
								<div field="functions" width="100%">权限</div>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<div style="text-align: center; padding: 10px;">
			<a class="mini-button" onclick="roleform.saveData"
				style="width: 60px; margin-right: 20px;">保存</a> <a
				class="mini-button" onclick="$.form.onCancel()" style="width: 60px;">取消</a>
		</div>
	</form>
</body>
<script type="text/javascript" src="${path}/js/scripts/dataform.js"></script>
<script type="text/javascript">
	mini.parse();
	var tree = mini.get("treegrid1");
	var showAllSelect = true;
	$(function() {
		$.form.init(); //初始化表单
		//设置选中节点
		window.roleform = {};
		roleform.saveData = function() {
			var tree = mini.get("treegrid1");
			var nodes = tree.getCheckedNodes();
			if (nodes.length < 1) {
				mini.alert("请选择要分配的资源！");
				return;
			}
			var datajson = getFuns();
			var roleid = mini.get("roleid").getValue();
			$.ajax({
				url : "${path}/role/mgr/addAllotResource",
				cache : false,
				post : "post",
				data : "datas=" + datajson + "&roleid=" + roleid,
				success : function(obj) {
					if (obj == "true") {
						mini.confirm("角色资源分配成功！要关闭窗口吗？", "友情提示", function(e) {
							if (e == 'ok') {
								return window.CloseOwnerWindow();
							}
						});
					}
				}
			});
		};
	});
	function ondrawcell(e) {
		var tree = e.sender, record = e.record, column = e.column, field = e.field, id = record[tree
				.getIdField()], funs = record.functions;
		function createCheckboxs(funs) {
			if (!funs)
				return "";
			var html = "";
			if (showAllSelect) {
				var value = record.checkAll !== false ? "全选" : "取消";
				var clickFn = 'checkAllFunc(\'' + id + '\', this)';
				html += '<input onclick="' + clickFn + '" type="button" value="' + value + '" style="border:solid 1px #aaa;"/>';
			}
			for (var i = 0, l = funs.length; i < l; i++) {
				var fn = funs[i];
				var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.code
						+ '\', this.checked)';
				var checked = fn.checked ? 'checked' : '';
				html += '<label class="function-item"><input onclick="' + clickFn
					+ '" ' + checked + ' type="checkbox" name="' + fn.code
					+ '" />'
						+ fn.functionName + '</label>';
			}
			return html;
		}
		if (field == 'functions') {
			e.cellHtml = createCheckboxs(funs);
		}
	}
	function getFuns() {
		var nodes = tree.getCheckedNodes();
		var roleid = mini.get("roleid").getValue();
		var datas = [];
		for (var i = 0; i < nodes.length; i++) {
			var roleResource = {};
			var ids = [];
			var actions = [];
			roleResource.sourceId = nodes[i].sourceId;
			roleResource.roleId = roleid;
			if (nodes[i].functions && nodes[i].functions.length > 0) {
				for (var j = 0; j < nodes[i].functions.length; j++) {
					if (nodes[i].functions[j].checked) {
						ids.push(nodes[i].functions[j].functionId);
						actions.push(nodes[i].functions[j].code);
					}
				}
				roleResource.functionIds = ids.join(",");
				roleResource.codes = actions.join(",");
			} else {
				roleResource.functionIds = "";
				roleResource.codes = "";
			}
			datas.push(roleResource);
		}
		var json = mini.encode(datas);
		return json;
	}
	function checkFunc(id, action, checked) {
		var tree = mini.get("treegrid1");
		var record = tree.getRecord(id);
		if (!record)
			return;
		var funs = record.functions;
		if (!funs)
			return;
		function getAction(action) {
			for (var i = 0; i < funs.length; i++) {
				var o = funs[i];
				if (o.code == action)
					return o;
			}
		}
		var obj = getAction(action);
		if (!obj)
			return;
		obj.checked = checked;
	}

	function load() {
		$
				.ajax({
					url : "${path}/role/mgr/showRoleResource?roleId=${param.roleid}",
					cache : false,
					async : false,//同步请求
					success : function(obj) {
						//obj.sidList.push(0); //调用checkNodes必须要多一个节点(bug);
						var tree = mini.get("treegrid1");
						var treeList = tree.getList(); //获取所有树节点
						var roleResource = {}; //已选中角色对应资源
						for (var i = 0,a=obj.length; i < a; i++) {
							var sid = obj[i].sourceId; //获取角色已选中资源id
							roleResource[sid] = obj[i];
						}

						//选中资源
						for (var j = 0; j < treeList.length; j++) {
							//已选中资源id与树中的资源id相等,标记选中
							var role_res = roleResource[treeList[j].sourceId];
							if (role_res) {
								treeList[j].checked = true;
								//角色资源的功能id 
								var role_res_fids = role_res.functionIds
										.split(",");
								var fids_map = {};
								for ( var a in role_res_fids) {
									fids_map[role_res_fids[a]] = role_res_fids[a];
								}
								var funs = treeList[j].functions;
								if (funs) {
									for (var k = 0; k < funs.length; k++) {
										if (fids_map[funs[k].functionId]) {
											funs[k].checked = true;
										}
									}
								}
							}
						}
					}
				});
	}
	//全选
	function checkAllFunc(id, btn) {
		var record = tree.getRecord(id);
		if (!record)
			return;
		var funs = record.functions;
		if (!funs)
			return;
		var checked = record.checkAll !== false;
		for (var i = 0, l = funs.length; i < l; i++) {
			var o = funs[i];
			o.checked = checked;
		}
		record.checkAll = !checked;
		tree.updateRow(record);
	}
</script>
</html>
